package com.example.klinedemo

import android.content.Context
import com.example.klinedemo.bean.DataResult
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.delay

/**
 * 模仿网络类
 */
class NetWork {


    suspend fun getKLineData(context: Context):DataResult{
        delay(200)
        var readAssetsFile = readAssetsFile(context, "stock_kline.json")
        val dataList = parseJson(readAssetsFile)
        return dataList
    }

    private fun parseJson(readAssetsFile: String): DataResult {
        val type = object : TypeToken<DataResult>() {}.type
        return Gson().fromJson(readAssetsFile, type) // 类型安全解析‌:ml-citation{ref="2,5" data="citationList"}
    }

    private fun readAssetsFile(context: Context, fileName: String): String {
        return context.assets.open(fileName).bufferedReader().use { it.readText() } // 同步读取‌:ml-citation{ref="5,7" data="citationList"}
    }

}